var deviceone = require("deviceone");
var cacher = deviceone.sm("do_DataCache");
var nf = deviceone.sm("do_Notification");
var common = require("common");
var http = require("helper/httpSilence");
var communityUrl = require("url/community");

var imgMax = 3;
/** 访问社区id */
var community_visitId = "communityVisitId";

/** 社区邻里时间戳 */
var community_message_timeup = "communityMessageTimeUp";
//高度
var community_height = "communityHeight";

module.exports.communityVisitId = function() {
	if (arguments.length == 0) {
		if (cacher.hasData(community_visitId)) {
			var value = cacher.loadData(community_visitId);
			if (!value || value == null || value === "undefined") {
				return "";
			}
			return value;
		}
	}
	if (arguments[0] === "") {
		cacher.removeData(community_visitId);
		return;
	}
	cacher.saveData(community_visitId, "" + arguments[0]);
};
module.exports.setJoinCommunityState = function(){
	cacher.saveData("joinCommunityState", "" + arguments[0]);
}
module.exports.getJoinCommunityState = function(){
	if (cacher.hasData("joinCommunityState")) {
		var value = cacher.loadData("joinCommunityState");
		return value;
	}
	return 0;
}
module.exports.communityMessageTimeUp = function() {
	var communityId = cacher.loadData(community_visitId);
	if (arguments.length == 0) {
		if (cacher.hasData(community_message_timeup+"_"+communityId)) {
			var value = cacher.loadData(community_message_timeup+"_"+communityId);
			if (!value || value == null || value === "undefined") {
				return "";
			}
			return value;
		}
	}
	if (arguments[0] === "") {
		cacher.removeData(community_message_timeup+"_"+communityId);
		return;
	}
	cacher.saveData(community_message_timeup+"_"+communityId, "" + arguments[0]);
};
module.exports.communityHeight = function() {
	if (arguments.length == 0) {
		if (cacher.hasData(community_height)) {
			var value = cacher.loadData(community_height);
			if (!value || value == null || value === "undefined") {
				return "";
			}
			return value;
		}
	}
	if (arguments[0] === "") {
		cacher.removeData(community_height);
		return;
	}
	cacher.saveData(community_height, "" + arguments[0]);
};
/**
 * ';'分隔图片字符串,转变成[{source:imgs[j],imgCount:""}]格式
 * 
 * @isShowAll:是否显示图片总数量，imgCount:共x张
 * @imgStr:img';'分隔图片字符串
 * @callback:回调函数
 */
var imgFormatFun = function(isShowAll, imgStr) {
	if (null == imgStr || '' == imgStr) {
		return null;
	}
	var imgs = imgStr.split(";");
	if (imgs.length <= 0) {
		return null;
	}
	// 图片的数据
	var jsonImgs = [];
	var imgsL = imgs.length;
	var showCount = "";
	if (imgsL > imgMax && isShowAll) {
		showCount = "共" + imgsL + "张";
		imgsL = imgMax;
	}
	// 图片的数据
	for (var j = 0; j < imgsL; j++) {
		if (j == (imgsL - 1) && isShowAll) {// 列表最后才显示共多少张
			jsonImgs.push({
				source : imgs[j],
				imgCount : showCount
			});
		} else {
			jsonImgs.push({
				source : imgs[j],
				imgCount : ""
			});

		}
	}
	return jsonImgs;
}
module.exports.fun = {
	img : {
		format : imgFormatFun
	}
};
module.exports.type = {
	/** 互助 * */
		HELP : 1,
	/** 八卦 * */
		GOSSIP : 2,
};
/**
 * 是否加入社区
 * communityId 社区id
 * trueBack 已加入社区执行函数
 * falseBack 未加入社区执行函数
 */
module.exports.isJoin = function(communityId,trueBack,falseBack) {
	if (!communityId || communityId == null || communityId == "undefined") {
		nf.toast("判断社区id不能为空。");
		return;
	}
	http.post(communityUrl.url.join.isJoin, {"communityId" : communityId}, function(data) {
		if(data.isJoin){
			if(trueBack && trueBack!=null && trueBack!=="undefined"){
				trueBack();
			}	
		}else{
			if(falseBack && falseBack!=null && falseBack!=="undefined"){
				falseBack()
			}
		}
	});
};

/**
 * 登陆用户是否有社区
 * trueBack 已加入社区执行函数
 * falseBack 未加入社区执行函数
 */
module.exports.hasJoin = function(trueBack,falseBack) {
	http.post(communityUrl.url.join.hasJoin, {}, function(data) {
		if(data.hasJoin){
			if(trueBack && trueBack!=null && trueBack!=="undefined"){
				trueBack();
			}	
		}else{
			if(falseBack && falseBack!=null && falseBack!=="undefined"){
				falseBack()
			}
		}
	});
};
module.exports.imgMax = imgMax;
